package com.qdxwx.manage.controller.website;

import com.alibaba.fastjson2.JSONObject;
import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.website.WebsiteProfileService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.enums.WebsiteProfileType;
import com.qdxwx.models.website.WebsiteProfile;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 配置信息表Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/profile")
public class WebsiteProfileController extends BaseController {

    @Autowired
    private WebsiteProfileService websiteProfileService;

    /**
     * 修改配置信息表
     */
    @GetMapping("/edit.html")
    public String edit(HttpServletRequest request, ModelMap model) {
        String type = request.getParameter("type");
        if (TKString.isEmpty(type)) type = WebsiteProfileType.serviceSwitch.toString();
        Map<String, Object> websiteProfile = websiteProfileService.getMap(type);
        if (websiteProfile == null) return error500;
        model.put("websiteProfile", websiteProfile);
        model.put("modelType", type);
        switch (type) {
            case "serviceSwitch":
                return "profile/service_switch";
            case "register":
                return "profile/register";
            case "baiJiaYun":
                return "profile/baijiayun";
            case "tencentCloud":
                return "profile/tencent_cloud";
            case "aliyun":
                return "profile/aliyun";
            case "genseeLive":
                return "profile/gensee_live";
            case "alipay":
                return "profile/alipay";
            case "wxPay":
                return "profile/wxpay";
            case "wxLogin":
                return "profile/wxlogin";
            case "invite":
                return "profile/invite";
            case "sensitiveWords":
                return "profile/sensitiveword";
            case "sso":
                return "profile/sso";
            default:
                return error404;
        }
    }

    /**
     * 修改保存配置信息表
     */
    @PostMapping("/save.json")
    @Log(title = "保存网站配置", businessType = BusinessType.UPDATE)
    @Permissions("profile:edit")
    @ResponseBody
    public AjaxResult editSave(HttpServletRequest request) {
        String type = request.getParameter("type");
        String description = request.getParameter("description");
        if (TKString.isEmpty(type) || TKString.isEmpty(description)) {
            return error();
        }
        WebsiteProfile websiteProfile = websiteProfileService.get(type);
        if (websiteProfile == null) {
            websiteProfile = new WebsiteProfile();
            websiteProfile.setType(type);
            websiteProfile.setDescription(description);
            websiteProfile.setExplain(type);
            return websiteProfileService.insert(websiteProfile) > 0 ? success() : error();
        } else {
            websiteProfile.setType(type);
            websiteProfile.setDescription(description);
            return websiteProfileService.update(websiteProfile) > 0 ? success() : error();
        }
    }

    @PostMapping("/sensitiveword.json")
    @Log(title = "保存敏感词配置", businessType = BusinessType.UPDATE)
    @Permissions("profile:edit")
    @ResponseBody
    public AjaxResult saveSensitiveWord(HttpServletRequest request) {
        List<String> badWords = new ArrayList<>();
        String tmpWords = request.getParameter("badWords");
        if (tmpWords != null) {
            String[] arrayWords = tmpWords.split("\n");
            for (String word : arrayWords) {
                word = word.trim();
                if (word.length() < 1) {
                    continue;
                }
                if (!badWords.contains(word)) {
                    badWords.add(word);
                }
            }
        }
        if(badWords.size() < 1) return error("敏感词不能为空");
        Map<String, String> config = new HashMap<>();
        config.put("badWords", String.join(",", badWords));
        WebsiteProfile websiteProfile = new WebsiteProfile();
        websiteProfile.setType(WebsiteProfileType.sensitiveWords.toString());
        websiteProfile.setDescription(JSONObject.toJSONString(config));
        int count = websiteProfileService.update(websiteProfile);
        return count > 0 ? success() : error();
    }
}
